package me.helllp.dake.core.component;



import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import me.helllp.dake.common.MessageCode;
import me.helllp.dake.common.base.BaseResult;
import me.helllp.dake.common.exception.DakeException;

import me.helllp.dake.core.utils.MessageUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常拦截
 */
@RestControllerAdvice
public class DakeGlobalExceptionHandler {
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler({DakeException.class})
    public String bindExceptionHandler(final DakeException e) {
        BaseResult result = BaseResult.result(e.getErrCode(), MessageUtils.message(e.getErrCode()), null);
        return JSON.toJSONString(result, JSONWriter.Feature.PrettyFormat);

    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(RuntimeException.class)
    public String handler(final RuntimeException e) {
        BaseResult result = BaseResult.result(MessageCode.ERROR_NORMAL, MessageUtils.message(MessageCode.ERROR_NORMAL), null);
        return JSON.toJSONString(result, JSONWriter.Feature.PrettyFormat);
    }
}
